package cn.wtg.kotlinlib.bindingAdapter

import android.graphics.Bitmap
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * author: wtg
 * date:2020/3/16 0016
 * desc: imageView的bindingAdapter
 */
object ImageBindingAdapter {
    /**
     * 加载图片
     *
     * @param imageView         imageView
     * @param binding_image_url          path
     * @param binding_image_placeholder 占位图片
     * @param binding_image_error  加载错误的图片
     */
    @JvmStatic
    @BindingAdapter(
        value = ["binding_image_url", "binding_image_placeholder", "binding_image_error"],
        requireAll = false
    )
    fun loadImage(
        imageView: ImageView,
        binding_image_url: Any?,
        @DrawableRes binding_image_placeholder: Int,
        @DrawableRes binding_image_error: Int
    ) {
        Glide.with(imageView).load(binding_image_url).placeholder(binding_image_placeholder)
            .error(binding_image_error)
            .into(imageView)
    }

    /**
     * 加载毛玻璃图片
     *
     * @param imageView         imageView
     * @param binding_burl_url  path
     * @param binding_image_placeholder 占位图片
     * @param binding_image_error  加载错误的图片
     */
    @JvmStatic
    @BindingAdapter(
        value = ["binding_burl_url", "binding_image_placeholder", "binding_image_error"],
        requireAll = false
    )
    fun loadBurlImage(
        imageView: ImageView,
        binding_burl_url: Any?,
        binding_image_placeholder: Int,
        binding_image_error: Int
    ) {
        Glide.with(imageView).load(binding_burl_url).placeholder(binding_image_placeholder)
            .error(binding_image_error)
            .apply(RequestOptions.bitmapTransform(BlurTransformation(25, 3)))
            .into(imageView)
    }

    /**
     * 加载图片资源文件
     *
     * @param imageView         imageView
     * @param binding_image_src 图片资源文件
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_image_src"], requireAll = false)
    fun loadImageSrc(imageView: ImageView, @DrawableRes binding_image_src: Int) {
        imageView.setImageResource(binding_image_src)
    }

    /**
     * 加载bitmap
     *
     * @param imageView         imageView
     * @param binding_bitmap_src  bitmap文件
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_bitmap_src"], requireAll = false)
    fun loadBitmapSrc(imageView: ImageView, binding_bitmap_src: Bitmap) {
        imageView.setImageBitmap(binding_bitmap_src)
    }
}